﻿using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.XImgProc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TemplateMatch
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCreateTemplates_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.Title = "Select an image with template...";
            openFileDialog1.Filter = "Image Files|*.jpg;*.png";
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                CreateTemplates frm = new CreateTemplates(openFileDialog1.FileName);
                frm.ShowDialog();
            }
        }

        private void btnSingleMatch_Click(object sender, EventArgs e)
        {
            frmTemplateMatch frm = new frmTemplateMatch();
            frm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image File|*.jpg;*.png";
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string f = openFileDialog1.FileName;
                string name = Path.GetFileNameWithoutExtension(f);
                string f1 = f.Replace(name, name + "_resized");
                Mat img = Cv2.ImRead(f);
                int w = img.Width * 4;
                int h = img.Height * 4;
                Mat img1 = img.Resize(new OpenCvSharp.Size(w, h));
                img1.SaveImage(f1);
            }    
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image File|*.jpg;*.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string f = openFileDialog1.FileName;
                Mat img = Cv2.ImRead(f);
                Mat dest = new Mat();

                //to gray
                Cv2.CvtColor(img, dest, ColorConversionCodes.BGR2GRAY);
                //to black and white
                Cv2.Threshold(dest, dest, 127, 255, ThresholdTypes.BinaryInv);
                //canny
                //Cv2.Canny(dest, dest, 50, 100);
                //Program.image_show("Canny", dest.ToBitmap());
                //line detector

                OpenCvSharp.XImgProc.FastLineDetector detector = CvXImgProc.CreateFastLineDetector(15, 2, 20, 20, 3, true);
                Vec4f[] lines = detector.Detect(dest);
                foreach(Vec4f line in lines)
                {
                    int x1 = (int)line.Item0;
                    int y1 = (int)line.Item1;
                    int x2 = (int)line.Item2;
                    int y2 = (int)line.Item3;
                    
                    if (Math.Abs(y2 - y1) < 2 && Math.Abs(x2 - x1) > 20)
                    {
                        img.Line((int)line.Item0, (int)line.Item1, (int)line.Item2, (int)line.Item3, Scalar.Green);
                    }
                    if (Math.Abs(y2 - y1) > 20 && Math.Abs(x2 - x1) < 2)
                    {
                        img.Line((int)line.Item0, (int)line.Item1, (int)line.Item2, (int)line.Item3, Scalar.Green);
                    }
                }

                Program.image_show("Lines", img.ToBitmap());
            }
        }
    }
}
